package com.hyh.shiyanshiguanli.service.impl;

import com.hyh.shiyanshiguanli.Expections.AccountNotFoundException;
import com.hyh.shiyanshiguanli.Expections.PasswordErrorException;
import com.hyh.shiyanshiguanli.constant.MessageConstant;
import com.hyh.shiyanshiguanli.domain.dto.*;
import com.hyh.shiyanshiguanli.domain.entity.Admin;
import com.hyh.shiyanshiguanli.domain.entity.Student;
import com.hyh.shiyanshiguanli.domain.vo.AdminVO;
import com.hyh.shiyanshiguanli.domain.vo.StudentVO;
import com.hyh.shiyanshiguanli.mapper.AdminMapper;
import com.hyh.shiyanshiguanli.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author hyh
 * @version 1.0
 */
@SuppressWarnings({"ALL"})
@Service
@Slf4j
public class AdminServiceImpl implements AdminService {

    @Autowired
    AdminMapper adminMapper;


    /**
     * 管理员登录
     * @param admin
     */
    @Override
    public Admin login(Admin adm) {
        String username = adm.getUsername();
        String password = adm.getPassword();
        Admin admin = adminMapper.selectByUserName(username);
        //账号不存在
        if(admin == null){
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        //密码比对
        if(!password.equals(admin.getPassword())){
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        return admin;
    }

    @Override
    public AdminVO page(MessageDTO messageDTO) {
        List<Admin> admins = adminMapper.page();
        int size = admins.size();
        AdminVO adminVO = new AdminVO();
        adminVO.setTotal(size);
        adminVO.setAdmins(admins);
        return adminVO;
    }


    @Override
    public void add(AdminAddDTO adminAddDTO) {
        adminMapper.add(adminAddDTO);
    }

    @Override
    public Admin selectById(Integer id) {
        return adminMapper.selectById(id);
    }

    @Override
    public void updateById(AdminUpdateDTO adminUpdateDTO) {
        adminMapper.updateById(adminUpdateDTO);
    }

    @Override
    public int deleteById(Integer id) {
        adminMapper.deleteById(id);
        return MessageConstant.SUCCESS;
    }
}
